package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.eiahe.hr.salary.domain.bo.EmployeePhoneAssociationBo;
import org.eiahe.hr.salary.domain.vo.EmployeePhoneAssociationVo;
import org.eiahe.hr.salary.service.IEmployeePhoneAssociationService;
import org.ehe.common.excel.utils.ExcelUtil;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 员工手机关联Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/phone")
@Slf4j
public class EmployeePhoneAssociationController extends BaseController {

    private final IEmployeePhoneAssociationService iEmployeePhoneAssociationService;

    /**
     * 查询员工手机关联列表
     */
    @SaCheckPermission("salary:phone:list")
    @GetMapping("/list")
    public TableDataInfo<EmployeePhoneAssociationVo> list(EmployeePhoneAssociationBo bo, PageQuery pageQuery) {
        return iEmployeePhoneAssociationService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取员工手机关联详细信息
     *
     * @param associationId 主键
     */
    @SaCheckPermission("salary:phone:query")
    @GetMapping("/{associationId}")
    public R<EmployeePhoneAssociationVo> getInfo(@PathVariable Long associationId) {
        return R.ok(iEmployeePhoneAssociationService.queryById(associationId));
    }

    /**
     * 新增员工手机关联
     */
    @SaCheckPermission("salary:phone:add")
    @Log(title = "员工手机关联", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmployeePhoneAssociationBo bo) {
        return toAjax(iEmployeePhoneAssociationService.insertByBo(bo));
    }

    /**
     * 修改员工手机关联
     */
    @SaCheckPermission("salary:phone:edit")
    @Log(title = "员工手机关联", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeePhoneAssociationBo bo) {
        return toAjax(iEmployeePhoneAssociationService.updateByBo(bo));
    }

    /**
     * 删除员工手机关联
     *
     * @param associationIds 主键串
     */
    @SaCheckPermission("salary:phone:remove")
    @Log(title = "员工手机关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{associationIds}")
    public R<Void> remove(@PathVariable Long[] associationIds) {
        return toAjax(iEmployeePhoneAssociationService.deleteWithValidByIds(Arrays.asList(associationIds)));
    }

    /**
     * 导出员工手机关联列表
     */
    @SaCheckPermission("salary:phone:export")
    @Log(title = "员工手机关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeePhoneAssociationBo bo, HttpServletResponse response) {
        List<EmployeePhoneAssociationVo> list = iEmployeePhoneAssociationService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工手机关联数据", EmployeePhoneAssociationVo.class, response);
    }

    /**
     * 验证员工手机号 - 短信验证码方式
     *
     * @param associationId 员工手机关联ID
     * @param captcha       短信验证码
     */
    @SaCheckPermission("salary:phone:verify")
    @Log(title = "员工手机关联", businessType = BusinessType.UPDATE)
    @PostMapping("/verify/captcha/{associationId}")
    public R<Void> verifyPhoneNumberByCaptcha(@PathVariable Long associationId, @RequestParam String captcha) {
        return toAjax(iEmployeePhoneAssociationService.verifyPhoneNumberByCaptcha(associationId, captcha));
    }

    /**
     * 验证员工手机号 - 人脸识别方式
     *
     * @param associationId 员工手机关联ID
     * @param faceData      人脸数据
     */
    @SaCheckPermission("salary:phone:verify")
    @Log(title = "员工手机关联", businessType = BusinessType.UPDATE)
    @PostMapping("/verify/face/{associationId}")
    public R<Void> verifyPhoneNumberByFace(@PathVariable Long associationId, @RequestBody String faceData) {
        return toAjax(iEmployeePhoneAssociationService.verifyPhoneNumberByFace(associationId, faceData));
    }

    /**
     * 检查员工手机号是否已验证
     *
     * @param employeeNo 工号
     */
    @SaCheckPermission("salary:phone:query")
    @GetMapping("/verify/status/{employeeNo}")
    public R<Boolean> isPhoneNumberVerified(@PathVariable String employeeNo) {
        return R.ok(iEmployeePhoneAssociationService.isPhoneNumberVerified(employeeNo));
    }
}
